﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace simpleDns
{
    public partial class dnsRecordConfig : Form
    {
        public DNSRECORD rcd;
        public dnsRecordConfig()
        {
            InitializeComponent();
            rcd = new DNSRECORD();
            rcd.enable = true;
            this.Text = "新建DNS记录";
        }
        public dnsRecordConfig(DNSRECORD d)
        {
            InitializeComponent();
            rcd = d;
            tbInfo.Text = d.info;
            tbName.Text = d.name;
            tbSort.Value = d.sort;
            rbEnable1.Checked = d.enable;
            rbEnable2.Checked = !d.enable;
            this.Text = "编辑DNS记录";
        }

        private void rbEnable2_CheckedChanged(object sender, EventArgs e)
        {
            rcd.enable = !rbEnable2.Checked;
        }

        private void rbEnable1_CheckedChanged(object sender, EventArgs e)
        {
            rcd.enable = rbEnable1.Checked;
        }
        private void tbInfo_TextChanged(object sender, EventArgs e)
        {
            rcd.info = tbInfo.Text.Trim();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            rcd.name = tbName.Text.Trim();
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            tbName.Focus();
            if (string.IsNullOrEmpty(tbName.Text.Trim()) || !settings.isIpAddress(tbName.Text.Trim()))
            {
                MessageBox.Show("地址不能为空，并且必须是IP地址");
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void tbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void tbSort_ValueChanged(object sender, EventArgs e)
        {
            rcd.sort =Convert.ToInt16(tbSort.Value);
        }
    }
}
